package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.ccl.iot.App;
import com.ccl.iot.privates.BitmapCollector;
import com.ccl.iot.privates.BitmapMask;
import com.ccl.iot.publics.UIUtils;

public class Aircondition8IconProvider extends DeviceIconProvider {

	public static boolean RegisterProviderType(){
		return DeviceIconProvider.RegisterProviderType(Aircondition8IconProvider.class);
	}

	public synchronized static Bitmap Build(int width, int height, int highLightColor, int backColor, boolean isOn, BitmapMask mask){
		if (width > 0 && height > 0){
			String key = "AirConditionIcon:" + width + "x" + height + ":" + highLightColor + "-" + backColor + (mask == null ? "" : "+" + mask.getHashCode());
			Bitmap bitmap = BitmapCollector.Get(key);
			if (bitmap== null){
				bitmap = Bitmap.createBitmap(width, height, backColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_8888);

				Canvas canvas = new Canvas(bitmap);

				Bitmap centerIcon = null;

				if(isOn){
					centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_AIR_OPEN[0]);
				} else {
					centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), ICON_LIST_AIR_CLOSE[0]);
				}

				int size = width > height ? height : width;

				centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

				float left = (width-centerIcon.getWidth())/2;
				float top = (height-centerIcon.getHeight())/2;


				if (backColor != Color.TRANSPARENT){
					canvas.drawColor(backColor);
				}

				canvas.drawBitmap(centerIcon, left, top, new Paint());

				if (mask != null){
					mask.paintMask(canvas);
				}

				canvas.save();
				canvas.restore();

				BitmapCollector.Add(key, bitmap);

				return bitmap;
			}else{
				return bitmap;
			}
		}

		return null;
	}

		//=========================================modify end=====================================================

		public static int GetHighLigthColor(boolean isOnline, boolean isOn, boolean isPressing){
			int tColor = Color.DKGRAY;

			if (isOnline){
				if (isOn){
					if (isPressing){
						tColor = Color.rgb(100, 200, 100);
					}else{
						tColor = Color.GREEN;
					}
				}else{
					if (isPressing){
						tColor = Color.rgb(200, 100, 100);
					}else{
						tColor = Color.RED;
					}
				}
			}

			return tColor;
		}

		public static int GetHighLigthColor(IOTAirCondition device, boolean isPressing){
			if (device != null){
				return GetHighLigthColor(device.isOnline(), device.isOn(), isPressing);
			}

			return Color.TRANSPARENT;
		}

		public static Bitmap Build(int style, int width, int height, int backColor, boolean isOnline, boolean isOn, boolean isPressing, int auth, int iconId){
			Bitmap icon = null;
			int color = GetHighLigthColor(isOnline, isOn, isPressing);

            icon = Build(width, height, color, backColor,isOn,null);

			return icon;
		}

		public static Bitmap Build(IOTAirCondition device, int style, int width, int height, int backColor, boolean isPressing){
			if (device != null){
				return Build(style, width, height, backColor, device.isOnline(), device.isOn(), isPressing, device.getAuth(), device.getSplitIconId());
			}

			return null;
		}


	/**
	 * No static function
	 */

	public Aircondition8IconProvider(IOTAirCondition8 device) {
		super(device);
	}

	@Override
	public Bitmap build(int style, int width, int height, int backColor, boolean isPressing) {
		return Build((IOTAirCondition)iDevice, style, width, height, backColor, isPressing);
	}

}
